package com.example.administrator.myokhttpmvp.net;

import android.util.Log;

import com.example.administrator.myokhttpmvp.utils.ToastUtil;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONObject;

import okhttp3.Call;

/**
 * Created by Administrator on 2018/9/25.
 */

public class NetCallBackImp extends StringCallback {

    @Override
    public void onError(Call call, Exception error, int id) {
        requestFinish();
        Log.e("Net:", error.getMessage() + "||||||||" + error.getCause());
        paresError("no", "网络或服务器异常，请稍后再试！");
    }

    @Override
    public void onResponse(String st, int id) {
        String statusCode = "";
        try {
            JSONObject response = new JSONObject(st) ;

            Log.d("=====tttt",response.toString());

            requestFinish();
            pareOkAll (response) ;

            if (response.has("code")) {
                statusCode = response.getString("code");
                pareOkCode(statusCode, response);


                if ("SUCCESS".equals(statusCode)) {
                    pareOk(response);
                    JSONObject data = null;
                    if (response.has("data")) {
                        data = response.getJSONObject("data");
                        pareOkData(data);
                    }

                } else if ("NOT_LOGIN".equals(statusCode)) {
//                    EventBus.getDefault().post(new UserEvent(UserEvent.LOGIN_TOLOGIN));
                }  else  {
                    Log.d("====sss",response.getString("msg"));
                    if (response.has("msg")) {
                        paresOther(statusCode, response.getString("msg"));
                    } else {
                        paresOther(statusCode, "服务器异常，请稍后再试！！");
                    }
                }
            }


        } catch (Exception e) {
            e.printStackTrace();
            paresError("no", "服务器异常，请稍后再试！");
        }
    }

    public void requestFinish() {

    }
    public void pareOkAll( JSONObject response) {
    }
    public void pareOkCode(String statusCode, JSONObject response) {
    }
    public void pareOk(JSONObject data) {
    }

    public void pareOkData(JSONObject data) {
    }
    public void paresOther(String code, String message) {

        Log.d("====message",message);
        ToastUtil.show(MyApplication.getAppContext(), message);

    }

    public void paresError(String code, String message) {
        ToastUtil.show(MyApplication.getAppContext(), message);
        paresOther(code, message);
    }



}